package com.xxgc.cloud.springcloudgateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.cloud.gateway.support.GatewayToStringStyler;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @program: SpringCloudStudy
 * @description: 空服务拦截
 * @author: Mr.abel(ShiJiaYi)
 * @create: 2022-11-29 16:55
 **/
@Component
@Slf4j
public class NullServiceGatewayFilterFactory extends AbstractNameValueGatewayFilterFactory {


    @Override
    public GatewayFilter apply(NameValueConfig config) {
        return new GatewayFilter(){
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                //获取请求对象
                ServerHttpRequest request = exchange.getRequest();
                //获取响应对象
                ServerHttpResponse response = exchange.getResponse();
                response.setStatusCode(HttpStatus.NOT_FOUND);
                return response.setComplete();
            }

            @Override
            public String toString() {
                return GatewayToStringStyler.filterToStringCreator(NullServiceGatewayFilterFactory.this).append(config.getName(), config.getValue()).toString();
            }
        };
    }
}
